package com.treetao.union.core.baidu.ai;


import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.treetao.union.core.baidu.ai.resp.BaiduFaceMatchResp;
import com.treetao.union.core.baidu.ai.resp.BaiduFaceRegResp;
import com.treetao.union.core.exception.DevMException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class BaiduFaceKit {
    private static AipFace client = null;
	
    /**
     * 每个项目只调用一次，不要重复调用
     * @param option
     * @return
     */
	public static AipFace me(BaiduFaceOption option) {
		if(option==null) {
			throw new DevMException("option required");
		}
		if(StringUtils.isEmpty(option.getAppId())) {
			throw new DevMException("appId cannot be empty");
		}
		if(StringUtils.isEmpty(option.getAppKey())) {
			throw new DevMException("appKey cannot be empty");
		}
		if(StringUtils.isEmpty(option.getSecretKey())) {
			throw new DevMException("secret cannot be empty");
		}
		// 初始化一个AipFace
        client = new AipFace(option.getAppId(), option.getAppKey(), option.getSecretKey());

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(option.getConnectTimeout());
        client.setSocketTimeoutInMillis(option.getSocketTimeout());

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
       // client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
       // client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
        return client;
	}
	
	/**
	 * 人脸注册
	 * @param imageBase64
	 * @param userId
	 * @return
	 */
	public  BaiduFaceRegResp userReg(String imageBase64,String userId) {
		checkRef();
		HashMap<String, String> options = new HashMap<String, String>();
	    options.put("user_info", "user's info");
	    options.put("quality_control", "NORMAL");
	    options.put("liveness_control", "NORMAL");
	    options.put("action_type", "REPLACE");
	    
		JSONObject result = client.addUser(imageBase64, "BASE64", "group1", userId, options);
		String json = result.toString();
		log.info("---人脸注册结果 >"+json);
		return JSON.parseObject(json, BaiduFaceRegResp.class);
	}
	
	/**
	 * 人脸更新
	 * @param imageBase64
	 * @param userId
	 * @return
	 */
	public  BaiduFaceRegResp userUpdate(String imageBase64,String userId) {
		checkRef();
		HashMap<String, String> options = new HashMap<String, String>();
	    options.put("user_info", "user's info");
	    options.put("quality_control", "NORMAL");
	    options.put("liveness_control", "NORMAL");
	    options.put("action_type", "REPLACE");
	    
		JSONObject result = client.updateUser(imageBase64, "BASE64", "group1", userId, options);
		String json = result.toString();
		log.info("---人脸更新结果 >"+json);
		return JSON.parseObject(json, BaiduFaceRegResp.class);
	}
	
	/**
	 * 人脸对比
	 * @param imageBase64
	 * @param userId
	 * @return
	 */
	public BaiduFaceMatchResp userMatch(String imageNewBase64,String imageOldFaceToken) {
		checkRef();
		String image1 = imageNewBase64;
	    String image2 = imageOldFaceToken;

	    // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
	    MatchRequest req1 = new MatchRequest(image1, "BASE64");
	    MatchRequest req2 = new MatchRequest(image2, "FACE_TOKEN");
	    ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
	    requests.add(req1);
	    requests.add(req2);

	    JSONObject result = client.match(requests);
	    String json = result.toString();
	    log.info("---人脸对比结果 >"+json);
	    return JSON.parseObject(json, BaiduFaceMatchResp.class);
	}
	
	private void checkRef() {
		if(client==null) {
			throw new DevMException("please call me() static method");
		}
	}
}
